package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-31 20:42
 **/
@RestController
@RequestMapping("member")
public class MemberController {

    @Reference
    private MemberService memberService;


    @RequestMapping("getMemberBeforeYear")
    public Result getMemberBeforeYear(){

        try {
            //全部月份放置在一个集合中
            LinkedList<String> allMonth = new LinkedList<>();
            //获取当前月份
            Date date = DateUtils.getToday();
            allMonth.add(DateUtils.parseDate2String(date, "yyyy-MM"));
            for (int i = 0; i < 11; i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.MONTH, -1);
                date = calendar.getTime();
                allMonth.add(DateUtils.parseDate2String(date, "yyyy-MM"));
            }
            Collections.reverse(allMonth);
            //全部数量放置在一个月份中
            LinkedList<Integer> allMember = new LinkedList<>();
            int count = 0;
            for (String month : allMonth) {
                count = memberService.getMemberByMonth(month);
                allMember.add(count);
            }
            HashMap<String, List> map = new HashMap<>();
            map.put("allMonth", allMonth);
            map.put("allMember", allMember);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }

//    public static void main(String[] args) throws Exception {
//        Date date = new Date();
//        Calendar instance = Calendar.getInstance();
//        instance.setTime(date);
//        System.out.println(instance.get(Calendar.MONTH));
//        System.out.println(date);
//        System.out.println(DateUtils.parseDate2String(date));
//    }


}
